<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("a").click(function () {
                var serializeVar = $(":hidden").serialize();
                var url = this.href + "&" + serializeVar;
                window.location.href = url;
                return false;
            });

            $("#pageNo").change(function () {
                var val = $(this).val();
                val = $.trim(val);

                //1.校验val是否为1,2，而不是a12，b等
                var reg = /^\d+$/g;
                if (!reg.test(val)) {
                    alert("输入页码不合法！");
                    $(this).val();
                    return;
                }

                //2.校验val在一个合法的范围内：1~totalPageNum
                var pageNo = parseInt(val);
                if (pageNo < 1 || pageNo > parseInt("${bookpage.totalPageNum}")) {
                    alert("不在页码范围内！");
                    $(this).val();
                    return;
                }

                //3.页面跳转
                var href = "bookServlet?method=getBooks&pageNo=" + pageNo + "&" + $(":hidden").serialize();
                window.location.href = href;
            });
        });
    </script>
</head>
<body>

<input type="hidden" name="minPrice" value="${param.minPrice}"/>
<input type="hidden" name="maxPrice" value="${param.maxPrice}"/>


<center>
    <c:if test="${param.title != null}">
        您已经将${param.title}放入到购物车中.
        <br/><br/>
    </c:if>
    <c:if test="${!empty sessionScope.ShoppingCart.books}">
        您的购物车中有${sessionScope.ShoppingCart.bookNumber}本书，<a href="bookServlet?method=toCartPage&pageNo=${bookpage.pageNo}">查看购物车</a>
    </c:if>

    <br/><br/>
    <form action="bookServlet?method=getBooks" method="post">
        Price:<input type="text" size="1" name="minPrice"/>
        -
        <input type="text" size="1" name="maxPrice"/>

        <input type="submit" value="查找"/>
    </form>

    <br/><br/>
    <table cellpadding="10">
        <tr>
            <td>书名/作者</td>
            <td>价格</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${bookpage.list}" var="book">
            <tr>
                <td>
                    <a href="bookServlet?method=getBook&pageNo=${bookpage.pageNo}&id=${book.id}">${book.title}</a>
                    <br/>
                        ${book.author}
                </td>
                <td>
                        ${book.price}
                </td>
                <td>
                    <a href="bookServlet?method=addToCart&pageNo=${bookpage.pageNo}&id=${book.id}&title=${book.title}">加入购物车</a>
                </td>
            </tr>
        </c:forEach>
    </table>
    <br/><br/>

    共${bookpage.totalPageNum}页
    &nbsp;&nbsp;
    当前第${bookpage.pageNo}页
    &nbsp;&nbsp;

    <c:if test="${bookpage.hasPrev}">
        <a href="bookServlet?method=getBooks&pageNo=1">首页</a>
        &nbsp;&nbsp;
        <a href="bookServlet?method=getBooks&pageNo=${bookpage.prevPage}">上一页</a>
    </c:if>

    &nbsp;&nbsp;

    <c:if test="${bookpage.hasNext}">
        <a href="bookServlet?method=getBooks&pageNo=${bookpage.nextPage}">下一页</a>
        &nbsp;&nbsp;
        <a href="bookServlet?method=getBooks&pageNo=${bookpage.totalPageNum}">末页</a>
    </c:if>
    &nbsp;&nbsp;
    转到<input type="text" size="1" id="pageNo"/>页
</center>
</body>
</html>
